﻿using System;
using System.Windows.Forms;
using System.Drawing;
using GMusicBox.Core;
using System.IO;

namespace GMusicBox
{
    public partial class frmPlayTip : PerPixelAlphaBlendForm
    {
        public frmPlayTip(Song s)
        {
            InitializeComponent();

            id = s.Album.Id;
            name = s.Name;
            artist = s.Artist.ToString();
            album = s.Album.Name;

            Draw(pictureBox1.Image);
            this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width - 10;
            this.Top = Screen.PrimaryScreen.Bounds.Bottom;
        }

        bool canclose = false;
        string id, name, artist, album;
        int add = -5;
        delegate void DrawCallBack(Image image);

        protected override bool ShowWithoutActivation
        {
            get
            {
                return true;
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000080;
                return cp;
            }
        }

        public void ImageOK()
        {
            string fileName = Helper.GetCachePath(Helper.CacheType.Thumbnail, id + ".jpg");
            if (File.Exists(fileName))
                Draw(new Bitmap(Helper.GetCachePath(Helper.CacheType.Thumbnail, id + ".jpg")));
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            timer2.Enabled = true;
        }

        private void Draw(Image image)
        {
            if (this.InvokeRequired)
                this.Invoke(new DrawCallBack(Draw));
            else
            {
                Bitmap b = new Bitmap(res.playing);
                Graphics g = Graphics.FromImage(b);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

                Font nFont = new Font("微软雅黑", 13);
                int dotw = TextRenderer.MeasureText("...", nFont).Width;
                bool deled = false;
                while (TextRenderer.MeasureText(name, nFont).Width > 250 - dotw)
                {
                    name = name.Substring(0, name.Length - 1);
                    deled = true;
                }
                g.DrawString(name.Trim() + (deled ? "..." : ""), nFont, Brushes.Black, new Point(124, 30));

                nFont = new System.Drawing.Font("微软雅黑", 10.5f);
                deled = false;
                while (TextRenderer.MeasureText(artist, nFont).Width > 250 - dotw)
                {
                    artist = artist.Substring(0, artist.Length - 1);
                    deled = true;
                }
                g.DrawString(artist.Trim() + (deled ? "..." : ""), new Font("微软雅黑", 10.5f), Brushes.Black, new PointF(124, 55));

                nFont = new System.Drawing.Font("微软雅黑", 9);
                deled = false;
                while (TextRenderer.MeasureText(album, nFont).Width > 250 - dotw)
                {
                    album = album.Substring(0, album.Length - 1);
                    deled = true;
                }
                g.DrawString(album.Trim() + (deled ? "..." : ""), new Font("微软雅黑", 9), Brushes.Black, new PointF(124, 80));
                g.DrawImage(image, 24, 4, 90, 90);

                this.SetBitmap(b);
                nFont.Dispose();
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            add = 5;
            timer2.Enabled = true;
            e.Cancel = !canclose;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top += add;
            if (this.Top <= Screen.PrimaryScreen.WorkingArea.Bottom - this.Height || this.Top >= Screen.PrimaryScreen.Bounds.Bottom)
            {
                timer2.Enabled = false;
                if (this.Top <= Screen.PrimaryScreen.WorkingArea.Bottom - this.Height) timer1.Enabled = true;
                if (this.Top >= Screen.PrimaryScreen.Bounds.Bottom) canclose = true;
            }
        }

        private void frmPlayTip_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close();
        }
    }
}
